﻿using System;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

namespace MikroTransfer
{
    public static class Methods
    {
        public static decimal ToCurrency(this string deger)
        {
            if (!string.IsNullOrEmpty(deger))
            {
                //NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
                //string sembol = LocalFormat.NumberDecimalSeparator;
                //if (sembol == ".")
                //{
                //  return Convert.ToDecimal(deger.Replace('.', ','));
                //}
                //else
                //{
                //  return Convert.ToDecimal(deger.Replace(',', '.'));
                //}
                return Convert.ToDecimal(deger.Replace('.', ','));
            }
            else
                return 0;
        }

        public static string ToFullSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", tarih.Year, tarih.Month, tarih.Day, tarih.Hour, tarih.Minute, tarih.Second);
        }

        public static string ToSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} 00:00:00", tarih.Year, tarih.Month, tarih.Day);
        }

        public static DateTime ToFullDateTime(this string tarih)
        {
            string[] date = tarih.Split(' ')[0].Split('-');
            string[] saat = tarih.Split(' ')[1].Split(':');
            return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]), int.Parse(saat[0]), int.Parse(saat[1]), int.Parse(saat[2]));
        }

        public static DateTime ToDateTime(this string tarih)
        {
            string[] date = tarih.Split(' ')[0].Split('-');
            return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]));
        }

        private static T CastTo<T>(this Object value, T targetType)
        {
            return (T)targetType;
        }

        public static List<T> CastToList<T>(this object obj, T prototype) where T : class
        {
            List<T> list = new List<T>();
            IEnumerable<T> enumerable = obj as IEnumerable<T>;
            if (enumerable != null)
            {
                list.AddRange(enumerable);
            }
            else
            {
                IEnumerable enumObjects = obj as IEnumerable;
                if (enumObjects == null)
                {
                    return null;
                }
                foreach (object enumObject in enumObjects)
                {
                    T currObject = CastTo(enumObject, prototype);

                    if (currObject == null)
                    {
                        list.Clear();
                        return list;
                    }
                    list.Add(currObject);
                }
            }
            return list;
        }

        //public static string Create()
        //{
        //    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        //    builder.DataSource = Program.Session[Settings.ServerName].ToString();
        //    builder.InitialCatalog = Program.Session[Settings.DatabaseName].ToString();
        //    builder.MultipleActiveResultSets = true;

        //    if (Program.Session[Settings.AuthenticateMode].ToString() == "Sql")
        //    {
        //        builder.UserID = Program.Session[Settings.UserName].ToString();
        //        builder.Password = Program.Session[Settings.Password].ToString();
        //        builder.IntegratedSecurity = false;
        //    }
        //    else
        //        builder.IntegratedSecurity = true;

        //    string providerString = builder.ToString();
        //    EntityConnectionStringBuilder ent = new EntityConnectionStringBuilder();
        //    ent.Provider = "System.Data.SqlClient";
        //    ent.ProviderConnectionString = providerString;
        //    ent.Metadata = @"res://*/Mikro.csdl|res://*/Mikro.ssdl|res://*/Mikro.msl";

        //    return ent.ToString();
        //}

    }
}
